#include "hplcqinfinedetectorprotocol.h"
#include "hplcqinfinedetectorsendpacket.h"
#include "hplcqinfinedetectorparsepacket.h"
#include "hplcqinfinedetectorprotocoldata.h"
#include "hplcqinfinedetectorparsesystempacket.h"
#include "hplcqinfinedetectorsendsystempacket.h"
#include "common/hplcqinfinemakepacket.h"

#include <QVariant>
#include "modulehplccmdid.h"

namespace hplc
{
namespace qinfine
{

HplcQinfineDetectorProtocol::HplcQinfineDetectorProtocol(DetectorParam &param, QObject *parent)
    : HplcQinfineDetectorProtocol (*new HplcQinfineDetectorProtocolData(), param, parent)
{

}

HplcQinfineDetectorProtocol::HplcQinfineDetectorProtocol(HplcQinfineDetectorProtocolData &data, DetectorParam &param, QObject *parent)
    : HplcQinfineProtocolCommon(data, param, parent)
    , m_param(&param)
{
    HplcQinfineDetectorParsePacket *parser = new HplcQinfineDetectorParsePacket(data, param, this);
    DetectorSendPacket *sender = new DetectorSendPacket(data, this);
    connect(sender, &HplcQinfineSendCommonPacket::requestSend, this, &HplcQinfineProtocolCommon::requestSend);

    HplcQinfineDetectorParseSystemPacket *systemParser = new HplcQinfineDetectorParseSystemPacket(data, param, this);
    HplcQinfineDetectorSendSystemPacket *systemSender = new HplcQinfineDetectorSendSystemPacket(data);

    data.m_parser = parser;
    data.m_sender = sender;
    data.m_systemParser = systemParser;
    data.m_systemSender = systemSender;
}

HplcQinfineDetectorProtocol::~HplcQinfineDetectorProtocol()
{
    HplcQinfineDetectorProtocolData *d = static_cast<HplcQinfineDetectorProtocolData*>(m_d);
    delete d->m_sender;
    delete d->m_parser;
    delete d->m_systemParser;
    delete d->m_systemSender;
}

bool HplcQinfineDetectorProtocol::setProtocolCmd(int id, QVariant &var)
{
    return HplcQinfineProtocolCommon::setProtocolCmd(id, var);
}

bool HplcQinfineDetectorProtocol::getProtocolCmd(int id, QVariant &var)
{
    return HplcQinfineProtocolCommon::getProtocolCmd(id, var);
}

DetectorSendPacket *HplcQinfineDetectorProtocol::getDetectorSender()
{
    return static_cast<DetectorSendPacket*>(m_d->m_sender);
}

HplcQinfineDetectorSendSystemPacket *HplcQinfineDetectorProtocol::getDetectorSystemSender()
{
    HplcQinfineDetectorProtocolData *m_d = static_cast<HplcQinfineDetectorProtocolData*>(this->m_d);
    return m_d->m_systemSender;
}

void HplcQinfineDetectorProtocol::setModel(quint8 model)
{
    m_param->deviceParam.model = static_cast<hplc::DetectorModel>(model);

    QVariant var;
    var.setValue(m_param->deviceParam.model);
    emit parseCompleted(hplc::CommonSystemCmdID_SetModel, var);
}

} // qinfine
} // hplc
